//
//  GAKKIHUD.m
//  GANGAIKit
//
//  Created by fh on 2019/11/5.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIHUD.h"
#import <MBProgressHUD/MBProgressHUD.h>

@interface GAKKIHUD ()
/** 第三方指示器 */
@property (nonatomic, strong) MBProgressHUD *hud;
@end

@implementation GAKKIHUD

#pragma mark - Public

/// 单例对象
+ (instancetype)shareGAKKIHUD {
    static GAKKIHUD *fhlbhub = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fhlbhub = [[self alloc] init];
    });
    return fhlbhub;
}

/// 展示提示文本到指定视图:弹框不消失,需要手动隐藏
/// @param infoText 提示文本
- (void)showLoadingViewWithInfo:(NSString *)infoText {
    [self showLoadingViewWithInfo:infoText toView:nil];
}

/// 展示提示文本到指定视图:弹框不消失,需要手动隐藏
/// @param infoText 提示文本
/// @param view 父视图
- (void)showLoadingViewWithInfo:(NSString *)infoText toView:(UIView *)view {
    [self showLoadingViewWithInfo:infoText toView:view cleanViewBeforeShow:NO];
}

/// 展示提示文本到指定视图:弹框不消失,需要手动隐藏
/// @param infoText 提示文本
/// @param view 父视图
/// @param shouldCleanViewBeforeShow 展示前是否移除已经存在的view
- (void)showLoadingViewWithInfo:(NSString *)infoText toView:(UIView *)view cleanViewBeforeShow:(BOOL)shouldCleanViewBeforeShow {
    if (@available(iOS 9.0, *)) {
        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
    }else {
        [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class],nil].color = [UIColor whiteColor];
    }
//    if (shouldCleanViewBeforeShow) {
//        [self hiddenLoadingView];
//    }
    if (!view) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    MBProgressHUD *hud = self.hud;
    if (!hud) {
        hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    }
    [self inner_commonMBProgressHUDConfigurationWithHub:hud];
    hud.mode = MBProgressHUDModeIndeterminate; 
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.detailsLabel.text = infoText;
    self.hud = hud;
}

/// 隐藏持久展示视图
- (void)hiddenLoadingView {
    [self.hud removeFromSuperview];
    self.hud = nil;
}


/// 展示提示文本:3秒后弹框自动消失
/// @param tipText 提示文本
- (void)showTipViewWithInfo:(NSString *)tipText {
    [self showTipViewWithInfo:tipText toView:nil];
}

/// 展示提示文本到指定视图:3秒后弹框自动消失
/// @param tipText 提示文本
/// @param view 需要展示的视图
- (void)showTipViewWithInfo:(NSString *)tipText toView:(UIView *)view {
    [self showTipViewWithInfo:tipText toView:view afterDelay:2.0];
}

/// 展示提示文本到指定视图:delay秒后弹框自动消失
/// @param tipText 提示文本
/// @param view 父视图
/// @param delay 消失时间
- (void)showTipViewWithInfo:(NSString * __nullable)tipText toView:(UIView * __nullable)view afterDelay:(NSTimeInterval)delay {
    [self hiddenLoadingView];
    if (!view) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = tipText;
    [self inner_commonMBProgressHUDConfigurationWithHub:hud];
    [hud hideAnimated:YES afterDelay:delay];
}

#pragma mark - Private

/// 公用配置
/// @param hud 控件
- (void)inner_commonMBProgressHUDConfigurationWithHub:(MBProgressHUD *)hud {
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8]; //0.45
    hud.minSize = CGSizeMake(120, 80);
    hud.detailsLabel.textColor = [UIColor whiteColor];
    hud.detailsLabel.font = [UIFont systemFontOfSize:17.0f];
    hud.removeFromSuperViewOnHide = YES;
}

@end


